package com.example.hrsystem.ui.approval.leave;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hrsystem.R;
import com.example.hrsystem.workflow.model.LeaveRequest;
import com.example.hrsystem.utils.ApprovalUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 请假申请列表适配器
 */
public class LeaveRequestAdapter extends RecyclerView.Adapter<LeaveRequestAdapter.LeaveRequestViewHolder> {
    
    private List<LeaveRequest> leaveRequests;
    private final OnLeaveRequestClickListener listener;

    /**
     * 点击监听器接口
     */
    public interface OnLeaveRequestClickListener {
        void onLeaveRequestClick(LeaveRequest leaveRequest);
    }

    /**
     * 构造函数
     * @param listener 点击监听器
     */
    public LeaveRequestAdapter(OnLeaveRequestClickListener listener) {
        this.leaveRequests = new ArrayList<>();
        this.listener = listener;
    }

    /**
     * 设置请假申请列表数据
     * @param leaveRequests 请假申请列表
     */
    public void setLeaveRequests(List<LeaveRequest> leaveRequests) {
        this.leaveRequests = leaveRequests != null ? leaveRequests : new ArrayList<>();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public LeaveRequestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 临时使用出差申请列表项布局文件
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.business_travel_request_item, parent, false);
        return new LeaveRequestViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull LeaveRequestViewHolder holder, int position) {
        LeaveRequest leaveRequest = leaveRequests.get(position);
        holder.bind(leaveRequest, listener);
    }

    @Override
    public int getItemCount() {
        return leaveRequests.size();
    }

    /**
     * 请假申请视图持有者
     */
    static class LeaveRequestViewHolder extends RecyclerView.ViewHolder {
        private final TextView requestNumberTextView;
        private final TextView leaveTypeTextView;
        private final TextView dateRangeTextView;
        private final TextView daysTextView;
        private final TextView statusTextView;
        private final TextView createTimeTextView;

        public LeaveRequestViewHolder(@NonNull View itemView) {
            super(itemView);
            requestNumberTextView = itemView.findViewById(R.id.request_number_text_view);
            leaveTypeTextView = itemView.findViewById(R.id.leave_type_text_view);
            dateRangeTextView = itemView.findViewById(R.id.date_range_text_view);
            daysTextView = itemView.findViewById(R.id.days_text_view);
            statusTextView = itemView.findViewById(R.id.status_text_view);
            createTimeTextView = itemView.findViewById(R.id.create_time_text_view);
        }

        public void bind(LeaveRequest leaveRequest, OnLeaveRequestClickListener listener) {
            // 设置请求编号 (暂时使用ID代替，因为没有requestNumber字段)
            requestNumberTextView.setText("请假申请 #" + leaveRequest.getId());
            
            // 设置请假类型
            if (leaveRequest.getLeaveType() != null) {
                leaveTypeTextView.setText(leaveRequest.getLeaveType());
            } else {
                leaveTypeTextView.setText("-");
            }
            
            // 设置日期范围
            if (leaveRequest.getStartTime() != null && leaveRequest.getEndTime() != null) {
                String dateRange = ApprovalUtils.formatDate(leaveRequest.getStartTime()) + " 至 "
                        + ApprovalUtils.formatDate(leaveRequest.getEndTime());
                dateRangeTextView.setText(dateRange);
            } else {
                dateRangeTextView.setText("-");
            }
            
            // 设置请假天数
            daysTextView.setText(String.valueOf(leaveRequest.getDays()) + " 天");
            
            // 设置状态
            if (leaveRequest.getStatus() != null) {
                statusTextView.setText(getStatusText(leaveRequest.getStatus()));
                // 根据状态设置不同的文本颜色
                if ("PENDING".equals(leaveRequest.getStatus())) {
                    statusTextView.setTextColor(itemView.getContext().getResources().getColor(R.color.colorPrimary));
                } else if ("APPROVED".equals(leaveRequest.getStatus())) {
                    statusTextView.setTextColor(itemView.getContext().getResources().getColor(R.color.colorSuccess));
                } else if ("REJECTED".equals(leaveRequest.getStatus())) {
                    statusTextView.setTextColor(itemView.getContext().getResources().getColor(R.color.colorError));
                } else {
                    statusTextView.setTextColor(itemView.getContext().getResources().getColor(R.color.colorTextSecondary));
                }
            } else {
                statusTextView.setText("-");
            }
            
            // 设置创建时间
            if (leaveRequest.getCreatedAt() != null) {
                createTimeTextView.setText("申请时间：" + ApprovalUtils.formatDateTime(leaveRequest.getCreatedAt()));
            } else {
                createTimeTextView.setText("申请时间：-");
            }
            
            // 设置点击事件
            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onLeaveRequestClick(leaveRequest);
                }
            });
        }

        /**
         * 根据状态代码获取状态文本
         * @param status 状态代码
         * @return 状态文本
         */
        private String getStatusText(String status) {
            switch (status) {
                case "PENDING":
                    return "待审批";
                case "APPROVED":
                    return "已批准";
                case "REJECTED":
                    return "已拒绝";
                case "CANCELLED":
                    return "已取消";
                default:
                    return status;
            }
        }
    }
}